package com.dcivision.forum.core;

/*
 * @(#)ForumManager.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.dcivision.forum.bean.ForumGroup;
import com.dcivision.forum.bean.ForumGroupDetail;
import com.dcivision.forum.dao.ForumGroupDAObject;
import com.dcivision.forum.dao.ForumGroupDetailDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;

/**
  ForumManager.java
  This class manage the general operation for forum discussion.
    @author           Zoe Shum
    @company          DCIVision Limited
    @creation date    23/10/2003
    @version          $Revision: 1.11 $
 */

public class ForumManager {

  public static final String REVISION = "$Revision: 1.11 $";

  SessionContainer sessionContainer;
  Connection conn;

  public ForumManager(SessionContainer sessionCon, Connection connection) {
    this.sessionContainer = sessionCon;
    this.conn = connection;
  }

  public void release() {
    this.sessionContainer = null;
    this.conn = null;
  }

  public ForumGroupDetail createForumGroupDetail(ForumGroupDetail forumGroupDetail) throws ApplicationException {
    ForumGroup forumGroup = new ForumGroup();
    ForumGroupDetail newForumGroupDetail = new ForumGroupDetail();

    ForumGroupDAObject forumGroupDAO = new ForumGroupDAObject(sessionContainer, conn);
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);

    Integer forumGroupID = forumGroupDetail.getForumGroupID();
    if (forumGroupID == null) {
      forumGroup = (ForumGroup) forumGroupDAO.getByGroupName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.FORUM_GROUP_DMS));
      //Temp case: for none froum group
      if (forumGroup == null) {
        forumGroup = new ForumGroup();
        forumGroup.setDescription("Default Forum Group for DMS");
        forumGroup.setGroupName(SystemParameterFactory.getSystemParameter(SystemParameterConstant.FORUM_GROUP_DMS));   //FOURM_GROUP_DMS
        forumGroup.setParentID(new Integer(0));
        forumGroup = (ForumGroup) forumGroupDAO.insertObject(forumGroup);
      }
      forumGroupID = forumGroup.getID();
      forumGroupDetail.setForumGroupID(forumGroupID);
    }
    newForumGroupDetail = (ForumGroupDetail) forumGroupDetailDAO.insertObject(forumGroupDetail);
    return newForumGroupDetail;
  }

  public ForumGroupDetail updateForumGroupDetail(ForumGroupDetail forumGroupDetail) throws ApplicationException {
    ForumGroupDetail newForumGroupDetail = new ForumGroupDetail();
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    newForumGroupDetail = (ForumGroupDetail) forumGroupDetailDAO.updateObject(forumGroupDetail);
    return newForumGroupDetail;
  }

  public void deleteForumGroupDetail(List forumGroupDetailList) throws ApplicationException {
    for (int i=0; i<forumGroupDetailList.size(); i++){
      ForumGroupDetail forumGroupDetail = (ForumGroupDetail)forumGroupDetailList.get(i);
      ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
      forumGroupDetailDAO.deleteObject(forumGroupDetail);
    }
  }
  
  public void hardDeleteForumGroupDetail(List forumGroupDetailList) throws ApplicationException {
    for (int i=0; i<forumGroupDetailList.size(); i++){
	  ForumGroupDetail forumGroupDetail = (ForumGroupDetail)forumGroupDetailList.get(i);
	  ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
	  forumGroupDetailDAO.hardDelete(forumGroupDetail);
	}
  }
  

  public boolean checkForumGroupExists(ForumGroup forumGroup) {

    return true;
  }

  public ForumGroupDetail getForumGroupDetail(Integer forumGroupDetailID) throws ApplicationException {
    ForumGroupDetail newForumGroupDetail = new ForumGroupDetail();
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    newForumGroupDetail = (ForumGroupDetail) forumGroupDetailDAO.getObjectByID(forumGroupDetailID);
    return newForumGroupDetail;
  }

  public List getForumGroupDetailListByObjectTypeObjectID(String objectType, Integer objectID) throws ApplicationException {
    return this.getForumGroupDetailListByObjectTypeObjectID(objectType,objectID,null);
  }
  
  public List getForumGroupDetailListByObjectTypeObjectID(String objectType, Integer objectID,Integer parentID) throws ApplicationException {
    List forumGroupDetailList = new ArrayList();
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    forumGroupDetailList = forumGroupDetailDAO.getForumGroupDetailListByObjectTypeObjectID(objectType, objectID,parentID);
    return forumGroupDetailList;
  }
  
  public List getInactiveForumGroupDetailListByObjectTypeObjectID(String objectType, Integer objectID) throws ApplicationException {
    List forumGroupDetailList = new ArrayList();
	ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
	forumGroupDetailList = forumGroupDetailDAO.getInactiveForumGroupDetailListByObjectTypeObjectID(objectType, objectID);
	return forumGroupDetailList;
  }

  public List getReplyList(Integer parentID, ForumGroupDetail forumGroupDetail) throws ApplicationException {
    List result = new ArrayList();
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    result = forumGroupDetailDAO.getReplyList(TextUtility.formatIntegerObj(parentID),
      forumGroupDetail.getObjectType(), TextUtility.formatIntegerObj(forumGroupDetail.getObjectID()));
    return result;
  }

  public Integer getReplyTotal(Integer forumGroupDetailID) throws ApplicationException {
    Integer result = new Integer(0);
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    result = forumGroupDetailDAO.getReplyTotal(forumGroupDetailID);
    return result;
  }

  public ForumGroupDetail getLastestReplied(ForumGroupDetail forumGroupDetail) throws ApplicationException {
    ForumGroupDetail tmpForumGroupDetail = new ForumGroupDetail();
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    tmpForumGroupDetail = forumGroupDetailDAO.getLastestReplied(TextUtility.formatIntegerObj(forumGroupDetail.getID()));
    return tmpForumGroupDetail;
  }
  
  public boolean hasDiscussion(String objectType, Integer objectID) throws ApplicationException{
    List dissList=this.getForumGroupDetailListByObjectTypeObjectID(objectType,objectID,new Integer(0));
    if(dissList.size()>0){
      return true; 
    }else{
      return false; 
    }
  }
  
  public int getTopicTotalByObject(String objectType,Integer objectID) throws ApplicationException {
    int total=0;
    List dissList=this.getForumGroupDetailListByObjectTypeObjectID(objectType,objectID,new Integer(0));
    if(!Utility.isEmpty(dissList)){
      total=dissList.size();
    }
    return total;
  }
  
  public int getReplyTotalByObject(String objectType,String objectID) throws ApplicationException {
    int total=0;
    ForumGroupDetailDAObject forumGroupDetailDAO = new ForumGroupDetailDAObject(sessionContainer, conn);
    List replyList=forumGroupDetailDAO.getReplyList(null,objectType,objectID);
    for(int i=0;i<replyList.size();i++){
      ForumGroupDetail forumGroupDetail=(ForumGroupDetail)replyList.get(i);
      try{
        ForumGroupDetail tmpForumGroupDetail=(ForumGroupDetail)forumGroupDetailDAO.getObjectByID(forumGroupDetail.getParentID());
      }catch(ApplicationException ee){
        if(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR.equals(ee.getMsgCode())){
          replyList.remove(forumGroupDetail); 
        }else{
          throw ee; 
        }
      }
    }
    if(!Utility.isEmpty(replyList)){
      total=replyList.size(); 
    }
    return total;
  }

}